package cn.amebius.simpledocker.controller;

import cn.amebius.simpledocker.controller.request.ContainerKeyRequest;
import cn.amebius.simpledocker.controller.request.ContainerRemoveRequest;
import cn.amebius.simpledocker.controller.request.ContainerRenameRequest;
import cn.amebius.simpledocker.entity.dto.DockerContainerDto;
import cn.amebius.simpledocker.entity.model.ContainerInfoModel;
import cn.amebius.simpledocker.service.ContainerService;
import cn.amebius.simpledocker.util.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.awt.*;

/**
 * 容器的操作接口
 * @author 阿伟
 * @date 2023/8/30 20:43
 */

@RestController
@RequestMapping("/api/container")
@Slf4j
public class ContainerController {

    @Autowired
    ContainerService containerService;

    @GetMapping("/getAll")
    public Response<DockerContainerDto> getAll() {
        DockerContainerDto dcd = containerService.getAll();
        return Response.ok(dcd);
    }

    @PostMapping("/infoById")
    public Response<ContainerInfoModel> getInfoById(@RequestBody ContainerKeyRequest containerKeyRequest) {
        ContainerInfoModel ret = containerService.getInfoById(containerKeyRequest.getKey(), containerKeyRequest.getId());
        return Response.ok(ret);
    }

    @PostMapping("/startContainer")
    public Response<String> startContainer(@RequestBody ContainerKeyRequest containerKeyRequest) {
        containerService.startContainer(containerKeyRequest.getKey(),containerKeyRequest.getId());
        return Response.ok("true");
    }

    @PostMapping("/stopContainer")
    public Response<String> stopContainer(@RequestBody ContainerKeyRequest containerKeyRequest) {
        containerService.stopContainer(containerKeyRequest.getKey(),containerKeyRequest.getId());
        return Response.ok("true");
    }

    @PostMapping("/pauseContainer")
    public Response<String> pauseContainer(@RequestBody ContainerKeyRequest containerKeyRequest) {
        containerService.pauseContainer(containerKeyRequest.getKey(),containerKeyRequest.getId());
        return Response.ok("true");
    }

    @PostMapping("/unpauseContainer")
    public Response<String> unpauseContainer(@RequestBody ContainerKeyRequest containerKeyRequest) {
        containerService.unpauseContainer(containerKeyRequest.getKey(),containerKeyRequest.getId());
        return Response.ok("true");
    }

    @PostMapping("/renameContainer")
    public Response<String> renameContainer(@RequestBody ContainerRenameRequest containerRenameRequest) {
        containerService.renameContainer(containerRenameRequest.getKey(),containerRenameRequest.getId(),containerRenameRequest.getName());
        return Response.ok("true");
    }

    @PostMapping("/removeContainer")
    public Response<String> removeContainer(@RequestBody ContainerRemoveRequest containerRemoveRequest) {
        containerService.removeContainer(containerRemoveRequest.getKey(),containerRemoveRequest.getId(),containerRemoveRequest.getForce(),containerRemoveRequest.getRemoveVolume());
        return Response.ok("true");
    }

}
